import grails.converters.JSON
class GameController {

    def index = { redirect(action: list, params: params) }


    static allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
        [gameInstanceList: Game.list(params), gameInstanceTotal: Game.count()]
    }
    def joinRed ={
        session.color=Piece.cRed
        redirect(action:show, id: params.id)
    }
    def joinBlack ={
        session.color=Piece.cBlack
        redirect(action:show, id: params.id)
    }

    def showBoard = {
        def gameInstance = Game.get(params.id)
        def pieceList= gameInstance.getBoard()
        [pieces:  pieceList]
    }
    def show = {
        def gameInstance = Game.get(params.id)
        if (!gameInstance) {
            flash.message = "Game not found with id ${params.id}"
            redirect(action: list)
        }
        else {
            def totalRounds = gameInstance.rounds.size()
            return [gameRounds: gameInstance.rounds, id:params.id]
        }
    }
    def addMove = {
        def gameInstance = Game.get(params.id)
        if (session.color != gameInstance.getNextPlayer()) {
            log.info "not your turn"
        } else {
            def newMove = new Move(params)
            if (gameInstance.addMove(newMove)) {
                newMove.save()
                if (session.color == Piece.cRed) {
                    def round = new Round()
                    round.save()
                    gameInstance.rounds.add(round)
                }
                gameInstance.rounds[-1].move(newMove)
            }else{
                log.info "=== wrong move ==="
            }
        }
        redirect(action: show, id: gameInstance.id)
    }

    def showLastRound = {
        def gameInstance = Game.get(params.id)
        if (gameInstance.rounds.size()>0) {
            def lastRound=  gameInstance.rounds[-1]
        }
        return [round: lastRound]
    }

    def nextPlayer = {
        def gameInstance = Game.get(params.id)
        def nextPlayer = gameInstance.getNextPlayer()
        return [player: nextPlayer]
    }

    def newMove = {
        def newMove = new Move()
        def playerList = Player.list();
        return [move: newMove, id: params.id, playerList: playerList]
    }

    def delete = {
        def gameInstance = Game.get(params.id)
        if (gameInstance) {
            try {
                gameInstance.delete()
                flash.message = "Game ${params.id} deleted"
                redirect(action: list)
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Game ${params.id} could not be deleted"
                redirect(action: show, id: params.id)
            }
        }
        else {
            flash.message = "Game not found with id ${params.id}"
            redirect(action: list)
        }
    }

    def create = {
        def gameInstance = new Game()
        gameInstance.properties = params
        return ['gameInstance': gameInstance]
    }

    def save = {
        def gameInstance = new Game(params)
        if (!gameInstance.hasErrors() && gameInstance.save()) {
            flash.message = "Game ${gameInstance.id} created"
            redirect(action: show, id: gameInstance.id)
        }
        else {
            render(view: 'create', model: [gameInstance: gameInstance])
        }
    }
}
